package com.example.rvdemo.type;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.rvdemo.R;

import java.util.List;

public class RVTypeAdaptar extends RecyclerView.Adapter{
    /**
     * 只有文字布局
     */
    public static final int TYPE_ONE_TEXT = 0;
    /**
     * 文字和图片布局
     */
    public static final int TYPE_TWO_TEXTANDIMG = 1;
    /**
     * 广告布局
     */
    public static final int TYPE_TREE_ADV = 2;
    private Context context;
//    private List<String> datas;

    private List<TypeBean> datas;

    public RVTypeAdaptar(Context context, List<TypeBean> datas) {
        this.context = context;
        this.datas = datas;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_ONE_TEXT){
            View view = LayoutInflater.from(context).inflate(R.layout.rv_type_item1, parent, false);
            return new TypeOneHolder(view);
        }else if (viewType == TYPE_TWO_TEXTANDIMG){
            View view = LayoutInflater.from(context).inflate(R.layout.rv_type_item2, parent, false);
            return new TypeTwoHolder(view);
        }else {
            View view = LayoutInflater.from(context).inflate(R.layout.rv_type_item3, parent, false);
            return new TypeThreeHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (holder.getItemViewType()){
            case TYPE_ONE_TEXT:
                TypeOneHolder holder1 = (TypeOneHolder) holder;
                holder1.textView.setText(datas.get(position).getDesc()+"    one");
                break;
            case TYPE_TWO_TEXTANDIMG:
                TypeTwoHolder holder2 = (TypeTwoHolder) holder;
                holder2.textView.setText(datas.get(position).getDesc()+"    two");
                Glide.with(context)
                        .load(datas.get(position).getImg1Url())
                        .into(holder2.iv1_type_2);
                Glide.with(context)
                        .load(datas.get(position).getImg2Url())
                        .into(holder2.iv2_type_2);
                Glide.with(context)
                        .load(datas.get(position).getImg3Url())
                        .into(holder2.iv3_type_2);
                break;
            case TYPE_TREE_ADV:
                TypeThreeHolder holder3 = (TypeThreeHolder) holder;
                holder3.textView.setText(datas.get(position).getDesc()+"    three");
                Glide.with(context)
                        .load(datas.get(position).getAdvImgUrl())
                        .into(holder3.iv_type_3);
            default:
                break;
        }
    }



    @Override
    public int getItemViewType(int position) {
        return Integer.parseInt(datas.get(position).getHolderType());
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class TypeOneHolder extends RecyclerView.ViewHolder {
        TextView textView;
        public TypeOneHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_type_1);
        }
    }

    public class TypeTwoHolder extends RecyclerView.ViewHolder {
        TextView textView;
        ImageView iv1_type_2,iv2_type_2,iv3_type_2;
        public TypeTwoHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_type_2);
            iv1_type_2 = itemView.findViewById(R.id.iv1_type_2);
            iv2_type_2 = itemView.findViewById(R.id.iv2_type_2);
            iv3_type_2 = itemView.findViewById(R.id.iv3_type_2);
        }
    }

    public class TypeThreeHolder extends RecyclerView.ViewHolder {
        TextView textView;
        ImageView iv_type_3;
        public TypeThreeHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_type_3);
            iv_type_3 = itemView.findViewById(R.id.iv_type_3);
        }
    }
}
